package com.zhoug.viewpermission;


import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限仓库
 *
 * @author: zhoug
 * @since: 2025-06-12
 */
public class MenuDaoHelper {

    private static MenuDao getMenuDao() {
        return MenuDatabase.getInstance().getPageMenuDao();
    }

    /**
     * 更新
     *
     * @param menus
     */
    public static void update(List<NavigationMenu> menus) {
        MenuDao dao = getMenuDao();
        dao.deleteAll();
        if (menus != null && !menus.isEmpty()) {
            dao.addAll(menus);
        }
    }

    /**
     * 查询所有
     *
     * @return
     */
    public static List<NavigationMenu> getAll() {
        MenuDao dao = getMenuDao();
        return dao.getAll();
    }

    /**
     * 根据id查询菜单
     *
     * @param id
     * @return
     */
    public static NavigationMenu getMenuById(String id) {
        if (TextUtils.isEmpty(id)) return null;
        MenuDao menuDao = getMenuDao();
        List<NavigationMenu> menus = menuDao.getMenuById(id);
        if (menus != null && !menus.isEmpty()) {
            return menus.get(0);
        }
        return null;
    }

    /**
     * 根据菜单组件地址查询菜单
     *
     * @param path
     * @return
     */
    public static NavigationMenu getMenuByPath(String path) {
        if (TextUtils.isEmpty(path)) return null;
        MenuDao menuDao = getMenuDao();
        List<NavigationMenu> navigationMenus = menuDao.getMenuByPath(path);
        if (navigationMenus != null && !navigationMenus.isEmpty()) {
            return navigationMenus.get(0);
        }
        return null;
    }

    /**
     * 根据parentId查询子菜单
     *
     * @param parentId
     * @return
     */
    public static List<NavigationMenu> getChildrenByParentId(String parentId) {
        MenuDao menuDao = getMenuDao();
        return menuDao.getChildrenByParentId(parentId);
    }

    /**
     * 根据parentPath查询子菜单
     *
     * @param parentPath
     * @return
     */
    public static List<NavigationMenu> getChildrenByParentPath(String parentPath) {
        MenuDao menuDao = getMenuDao();
        List<NavigationMenu> menuByPath = menuDao.getMenuByPath(parentPath);
        if (menuByPath != null && !menuByPath.isEmpty()) {
            return menuDao.getChildrenByParentId(menuByPath.get(0).getId());
        }
        return null;
    }

    public static List<NavigationMenu> getAllChildrenByParentId(String parentId) {
        return getChildrenByParentId(parentId, Integer.MAX_VALUE);
    }

    /**
     * 递归查询所有子菜单
     *
     * @param parentId
     * @return
     */
    public static List<NavigationMenu> getChildrenByParentId(String parentId, int level) {
        MenuDao menuDao = getMenuDao();
        List<NavigationMenu> children = menuDao.getChildrenByParentId(parentId);
        if (children != null && !children.isEmpty() && level >= 2) {
            for (NavigationMenu item : children) {
                List<NavigationMenu> children1 = getChildrenByParentId(item.getId(), level - 1);
                item.setChildren(children1);
            }
        }
        return children;
    }

    public static List<NavigationMenu> getAllChildrenByParentPath(String parentPath) {
        return getChildrenByParentPath(parentPath, Integer.MAX_VALUE);
    }

    /**
     * 递归查询所有子菜单
     *
     * @param parentPath
     * @return
     */
    public static List<NavigationMenu> getChildrenByParentPath(String parentPath, int level) {
        List<NavigationMenu> children = getChildrenByParentPath(parentPath);
        if (children != null && !children.isEmpty() && level >= 2) {
            for (NavigationMenu item : children) {
                List<NavigationMenu> children1 = getChildrenByParentId(item.getId(), level - 1);
                item.setChildren(children1);
            }
        }
        return children;
    }


    /**
     * 查询根节点菜单
     *
     * @return
     */
    public static List<NavigationMenu> getRootMenus() {
        MenuDao menuDao = getMenuDao();
        return menuDao.getRootMenus();
    }

    /**
     * 根据id查询权限
     *
     * @param id
     * @return
     */
    public static List<NavigationMenu> getPermissionsByMenuId(String id) {
        MenuDao menuDao = getMenuDao();
        return menuDao.getPermissionsByMenuId(id);
    }

    /**
     * 根据权限标志查询权限
     *
     * @param permissionKey
     * @return
     */
    public static List<NavigationMenu> getPermissionsByKey(String permissionKey) {
        MenuDao menuDao = getMenuDao();
        return menuDao.getPermissionsByKey(permissionKey);
    }

    /**
     * 根据权限标志查询权限
     *
     * @param keys
     * @return
     */
    public static List<ViewPermission> getPermissionsByKeys(List<String> keys) {
        if (keys == null || keys.isEmpty()) return null;
        List<ViewPermission> result = new ArrayList<>();
        for (String key : keys) {
            List<NavigationMenu> menus = MenuDaoHelper.getPermissionsByKey(key);
            ViewPermission permission = new ViewPermission(key);
            MenuRepository.bindPermission(permission, menus);
            result.add(permission);
        }
        return result;
    }



}
